package first_practice;

import java.util.HashSet;
import java.util.Set;

/**
 * @author ZhiWen Ren
 * @version 1.0
 * @description: LeetCode-3-无重复字符的最长字串
 * @date 2025/3/13 14:48
 */
public class Solution3 {
    public static void main(String[] args) {
        String s = "pwwkew";
        lengthOfLongestSubstring(s);
    }

    public static int lengthOfLongestSubstring(String s) {
        // right不需要左移
        int n = s.length(), ans = 0, right = 0;
        Set<Character> set = new HashSet<>();
        for (int i = 0; i < n; i++) {
            if (i > 0) {// i大于0，说明子串有元素重复了！
                set.remove(s.charAt(i-1));
            }
            while (right < n && !set.contains(s.charAt(right))) {
                set.add(s.charAt(right));
                ++right;
            }
            ans = Math.max(ans, set.size());
            if (right == n) return ans;
        }
        return ans;
    }
}
